package cn.com.gridinfo.sooc.core.my.msg.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.StringUtils;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.HashMap;
import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.course.discuss.activity.DiscussInfoActivity;
import cn.com.gridinfo.sooc.core.my.msg.activity.NoticeDetailActivity;

/**
 * Created by Michelle on 2016/1/6.
 */
public class NoticeMsgAdapter extends UltimateViewAdapter<NoticeMsgAdapter.ViewHolder> {
    private Context context;
    private List<HashMap<String, Object>> noticeList;

    public NoticeMsgAdapter(Context context, List<HashMap<String, Object>> noticeList) {
        this.context = context;
        this.noticeList = noticeList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.my_msg_notice_item,parent,false);
        return new ViewHolder(view,true);
    }

    @Override
    public int getAdapterItemCount() {
        return noticeList != null?noticeList.size():0;
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position<getItemCount()&&(customHeaderView!=null?
                position<=noticeList.size():position<noticeList.size())&&
                (customHeaderView!=null?position>0:true)){
            final HashMap<String, Object> noticeInfo=noticeList.get(position);
            holder.title.setText(noticeInfo.get("noticeTitle").toString());
            holder.content.setText(noticeInfo.get("noticeDec").toString());
            if (StringUtils.isNull(noticeInfo.get("createTime").toString())){
                holder.time.setText("无");
            }else {
                holder.time.setText(noticeInfo.get("createTime").toString());
            }
            holder.rlToLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("url", noticeInfo.get("noticeUrl")+"");
                    intent.setClass(context, NoticeDetailActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        //通知名称、内容、时间
        TextView title,content,time;
        //查看详情
        RelativeLayout rlToLook;

        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem) {
                //控件布局
                title = (TextView) itemView.findViewById(R.id.my_msg_notice_item_title);//课程名称
                content = (TextView) itemView.findViewById(R.id.my_msg_notice_item_content);//课程名称
                time = (TextView) itemView.findViewById(R.id.my_msg_notice_item_time);// 上次学习时间
                rlToLook = (RelativeLayout) itemView.findViewById(R.id.my_msg_notice_item_to_look);
            }
        }
    }

    public void loadmore(List<HashMap<String, Object>> list){
        noticeList.addAll(list);
        notifyDataSetChanged();

    }
    public void refresh(List<HashMap<String, Object>> list){
        clear(noticeList);
        noticeList.addAll(list);
        notifyDataSetChanged();
    }
}


